package leetcode_1001_1100;

import java.security.Key;
import java.util.Arrays;

public class LeeCode_1027 {
    public static void main(String[] args) {
        System.out.println(longestArithSeqLength(new int[]{3, 6, 9, 12}));
    }
    private static int longestArithSeqLength(int[] nums) {
        int n = nums.length;
        int[][] dp = new int[n][1001];
        for (int i = 0; i < n; i++) {
            Arrays.fill(dp[i], 1);
        }
        int max = 0;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                int t = nums[i] - nums[j] + 500;
                dp[i][t] = dp[j][t] + 1;
                max = Math.max(dp[i][t], max);
            }
        }
        return max;
    }
    private static int longestArithSeqLength2(int[] nums) {
        int ans = 1;
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int dis = nums[i] - nums[j];
                int bef = nums[j];
                int count = 2;
                for (int k = j + 1; k < n; k++) {
                    if (bef - nums[k] == dis) {
                        count++;
                        bef = nums[k];
                    }
                }
                ans = Math.max(ans, count);
            }
        }
        return ans;
    }
}
